/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.socialNet.dao.implementations;

import com.bionic.socialNet.dao.interfaces.IContactDAO;
import com.bionic.socialNet.entities.ContactData;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ann
 */
public class OraContact extends QueryExecutor implements IContactDAO {

    private final String SELECT_CONTACT = "select contact_id, type, value, user_id from contact where contact_id = %d";
    private final String UPDATE_CONTACT = "  update contact set type = %d,   value = '%s',  user_id = %d "
            + " where contact_id = %d";
    private final String DELETE_CONTACT = " delete contact  where contact_id = %d";
    private final String SELECT_CONTACTS_LIST = "";
    private final String INSERT_CONTACT = "insert into contact  (contact_id, type, value, user_id)"
            + " values  (SEQ_CONTACT.Nextval, %d, %s, %d);";


    @Override
    public boolean insertContact(ContactData contact) {
        return query.executeInsertStatement(String.format(SELECT_CONTACT, contact.getTYPE(), contact.getVALUE(), contact.getUSER_ID()));
    }

    @Override
    public boolean deleteContact(int contactID) {
        return query.executeDeleteStatement(String.format(DELETE_CONTACT, contactID));
    }

    @Override
    public boolean updateContact(ContactData contact) {
        // update contact set type = %d,   value = '%s',  user_id = %d 
        return query.executeUpdateStatement(String.format(UPDATE_CONTACT, contact.getTYPE(), contact.getVALUE(), contact.getUSER_ID(), contact.getCONTACT_ID()));
    }

    @Override
    public Collection<ContactData> selectContactssCollection(int userID) {
        ResultSet RS = query.getRSForQuery(String.format(null));

        ArrayList<ContactData> contacts = new ArrayList<ContactData>();
        try {
            while (RS.next()) {
                ContactData contact = new ContactData();
                contact.setCONTACT_ID(RS.getInt("contact_id"));
                contact.setTYPE(RS.getInt("type"));
                contact.setVALUE(RS.getString("value"));
                contact.setUSER_ID(RS.getInt("user_id"));

                contacts.add(contact);
            }
        } catch (SQLException ex) {
            Logger.getLogger(OraContact.class.getName()).log(Level.SEVERE, null,
                    ex);
            return null;
        }
        return contacts;
    }
}
